package com.sky.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.sky.domain.User;
import com.sky.service.UserService;
import com.sky.utils.PassWordutil;

public class RegisterServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("=eeeee===");
		//1.获取请求参数
		Map<String,String[]> map  =  request.getParameterMap();
		//把map中的请求数据封装到User对象中
		//(涉及到jar: 1.commons-beanutils-1.8.3.jar:封装参数到javaBean实体类中的工具类
		//          2.commons-logging-1.1.1.jar:工具类的日志)
		User user =new User();
		try {
			BeanUtils.populate(user, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//设置激活码(随机字符串)
		user.setActiveCode(UUID.randomUUID().toString());
		System.out.println("=dddd==="+user.toString());

		
		//先要判断中文校验码，
		//成功了就可以进行后续的插入数据库和发送邮箱激活码了
		//从绘制的时候存储的session中取出存的成语
		String checkcode =  (String) request.getSession().getAttribute("checkcode_session");
		//取出用户输入的校验码成语
		String ckCode =request.getParameter("ckCode");
		/*//容易出现空指针，输入内容为空的话
		ckCode.equals(checkcode);*/
		
		if(checkcode.equals(ckCode)){
			
			//判断输入的两次
			//1.获取确认密码框的内容
			String confirmPwd=request.getParameter("rpassword");
			String pwd = user.getPassword();
			if("".equals(pwd)||null==pwd){
				request.setAttribute("error_msg", "密码不能为空");
				request.getRequestDispatcher("register.jsp").forward(request, response);
			}else{
				 //判断两次密码是否一致
				if(user.getPassword().equals(confirmPwd)){
					//2.处理业务逻辑(根据请求参数去处理得到想要的结果)	  用户业务(注册，登录)
					UserService userService =new UserService();
					try {
					//=========================
//						加密
						String pwd2 =user.getPassword();
						try {
							PassWordutil des = new PassWordutil();
							user.setPassword(des.encrypt(pwd2));
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
					//=========================
						boolean isSuccess = 	userService.registerUser(user);  
					//3.分发转向(页面跳转)
					 if (isSuccess) {
						//跳转到注册成功页面
						 request.getRequestDispatcher("registersuccess.jsp").forward(request, response);
					}else{
						//重新跳转到注册页面重新输入
						request.setAttribute("error_msg", "注册失败");
						request.getRequestDispatcher("register.jsp").forward(request, response);
					}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						//重新跳转到注册页面重新输入
						request.setAttribute("error_msg", "注册失败");
						request.getRequestDispatcher("register.jsp").forward(request, response);
					}
				}else{
					request.setAttribute("error_msg", "两次密码不一致");
					request.getRequestDispatcher("register.jsp").forward(request, response);
				}
			}
			
			
			
			  
			
		}else{
			System.out.println("==验证码校验失败==");
			request.setAttribute("code_msg", "验证码输入错误");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		
		
	
	
   
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}


}
